<template>
  <div>
    <Card>
      <Collapse style="margin-bottom:15px;">
        <Panel>查询
          <div slot="content">
            <Form
              ref="formLkyTeacherReleaseRecord"
              :model="formLkyTeacherReleaseRecord"
              label-position="right"
              :label-width="80"
              inline
            >
              <!--学院、专业、班级查询-->
              <FormItem label="所属学院" prop="name1">
                <Select v-model="coid" style="width:200px"  @on-open-change="onOpenChangeCollege" @on-change="onOpenChangeMajor" clearable >
                  <Option v-for="item in collegeList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="所属专业" prop="name2">
                <Select v-model="mid" style="width:200px"  @on-change="onOpenFindClassByMid"clearable >
                  <Option v-for="item in majorList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="所属班级" prop="name3" >
                <Select v-model="cid"   style="width:200px" clearable >
                  <Option v-for="item in classList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem>
                <Button type="primary" @click="handleSubmit('formLkyTeacherReleaseRecord')">查询</Button>
                <Button @click="handleReset('formLkyTeacherReleaseRecord')" style="margin-left: 8px">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Panel>
      </Collapse>
      <Table
        ref="tableLkyTeacherReleaseRecord"
        :data="tableData"
        :columns="columns"
        @on-selection-change="onSelectionChange"
        @on-sort-change="onSortChange"
      />
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page
            :total="total"
            :current="current"
            :page-size="pageSize"
            @on-page-size-change="changePageSize"
            @on-change="changePage"
            :show-total="true"
            show-sizer
          ></Page>
        </div>
      </div>
    </Card>
  </div>
</template>

<script>
import { list, batchDelete } from '@/api/lky/lkyTeacherReleaseRecord'
import { createFormObj, getToken } from '@/libs/util'
// 学院查询、专业、班级查询
import { queryAll } from '@/api/lky/lkyCollege'
import { queryAllM } from '@/api/lky/lkyMajor'
import { queryFindClassByMid } from '@/api/lky/lkyClass'

export default {
  name: 'LkyTeacherReleaseRecord',
  components: {

  },
  data () {
    return {
      // 存储id
      formLkyTeacherReleaseRecord: {
        cid: '',
        mid: '',
        coid: ''
      },
      columns: [
        { type: 'selection', width: 60, align: 'center' },
				{ key: 'name3', title: '老师', sortable: true },
				{ key: 'job_number', title: '工号', sortable: true },
				{ key: 'name', title: '班级', sortable: true },
        { key: 'name1', title: '专业', sortable: true },
        { key: 'name2', title: '学院', sortable: true },
        {
          title: '操作',
          key: 'action',
          width: 250,
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
					type: 'primary',
                    size: 'small'
                  },
                  style: {
                    marginRight: '5px'
                  },
                  on: {
                    click: () => {
                      this.classInfo = params.row
                      console.log(params.row.id)
                      this.$router.push({
                        path: '/releaseTask/release',
                        query: {
                          cid: params.row.id
                        }
                      })
                    }
                  }
                },
                '发布任务'
              )
            ])
          }
        }
      ],
      tableData: [],
      tid: '',
      total: 0,
      current: 1,
      pageSize: 10,
      isOpenReleaseRecordPage: false,
      isOpenEditPage: false,
      editLkyTeacherReleaseRecord: {},
      selection: [],
      coid: '',
      mid: '',
      cid: '',
      collegeList: [],
      majorList: [],
      classList: [],
      classInfo: []
    }
  },
  methods: {
    onOpenChangeCollege () {
      queryAll().then(res => {
        this.collegeList = res.data
      })
    },
    onOpenChangeMajor (coid) {
      this.cid = ''
      this.mid = ''
      queryAllM({ 'coid': this.coid }).then(res => {
        this.majorList = JSON.parse(JSON.stringify(res.data))
      })
    },
    onOpenFindClassByMid (mid) {
      queryFindClassByMid({ 'mid': this.mid }).then(res => {
        this.classList = JSON.parse(JSON.stringify(res.data))
      })
    },
    handleSubmit (name) {
      this.getLkyTeacherReleaseRecordList(this.current, this.pageSize)
      this.$Message.success('Success!')
    },
    handleReset (name) {
      this.cid = ''
      this.mid = ''
      this.coid = ''
      this.$refs[name].resetFields()
    },
    handleOpenPage (name) {
      // 打开页面事件(添加页,编辑页...)
      if (name === 'ReleaseRecord') this.isOpenReleaseRecordPage = true
      else if (name === 'edit') {
        this.isOpenEditPage = true
      }
    },
    addLkyTeacherReleaseRecord (row) {
      this.tableData.push(row)
    },
    updateLkyTeacherReleaseRecord (row) {
      this.$set(this.tableData, row._index, row)
    },
    toReleaseRecordInfo (row) {
      this.$set(this.tableData, row._index, row)
    },
    batchDelete () {
      this.$Modal.confirm({
        title: '提示',
        content: '<p>确定删除吗?</p>',
        onOk: () => {
          if (this.selection.length > 0) {
            let ids = ''
            for (var i = 0; i < this.selection.length; i++) {
              ids += ',' + this.selection[i].id
            }
            batchDelete({ ids: ids }).then(res => {
              for (var i = 0; i < this.selection.length; i++) {
                this.tableData = this.tableData.filter(
                  item => item.id !== this.selection[i].id
                )
              }
            })
          } else {
            this.$Message.error('未选中记录!')
          }
        },
        onCancel: () => {}
      })
    },
    getLkyTeacherReleaseRecordList (current, pageSize, paras) {
      let para = { pageNumber: current, pageSize: pageSize }
      if (paras) Object.assign(para, paras)
      // key-value放入传入后端
      Object.assign(para, { tid: this.tid, coid: this.coid, mid: this.mid, cid: this.cid })
      console.log('para' + para)
      list(this.handleRequestParams(para)).then(res => {
        this.tableData = res.list
        this.current = parseInt(res.pageNumber)
        this.pageSize = parseInt(res.pageSize)
        this.total = parseInt(res.totalRow)
      })
    },
    handleRequestParams (para) {
      Object.assign(para, createFormObj(this.formLkyTeacherReleaseRecord, 'lkyTeacherReleaseRecord'))
      return para
    },
    exportExcel () {
      this.$refs.tableLkyTeacherReleaseRecord.exportCsv({
        filename: `table-${new Date().valueOf()}.csv`
      })
    },
    changePage (pageNumber) {
      this.getLkyTeacherReleaseRecordList(pageNumber, this.pageSize)
    },
    changePageSize (pageSize) {
      this.getLkyTeacherReleaseRecordList(this.current, pageSize)
    },
    remove (index) {
      this.data6.splice(index, 1)
    },
    onSelectionChange (selection) {
      this.selection = selection
    },
    onSortChange (obj) {
      if (obj.order === 'normal') {
        this.getLkyTeacherReleaseRecordList(1, this.pageSize)
      } else {
        this.getLkyTeacherReleaseRecordList(1, this.pageSize, {
          field: obj.key,
          order: obj.order
        })
      }
    }
  },
  mounted () {
    this.tid = getToken()
    this.getLkyTeacherReleaseRecordList(this.current, this.pageSize)
  }
}
</script>

<style>
</style>
